//
//  WFSettingHeaderView.m
//  WisdomFarm
//
//  Created by sky on 2017/10/25.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFSettingHeaderView.h"
#import <BlocksKit/UIControl+BlocksKit.h>
#import "WFLoginViewController.h"

@interface WFSettingHeaderView ()

@property (nonatomic, strong) UIImageView *backgroundView;
@property (nonatomic, strong) UIImageView *avatarBackgroundView;
@property (nonatomic, strong) UIImageView *avatarView;
@property (nonatomic, strong) UILabel *mobileLabel;

@end

@implementation WFSettingHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _backgroundView = [[UIImageView alloc] init];
        [self addSubview:_backgroundView];
        [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.insets(UIEdgeInsetsZero);
        }];
        _backgroundView.image = [UIImage imageNamed:@"about_bg"];
        _backgroundView.contentMode = UIViewContentModeScaleAspectFill;
        
        _avatarBackgroundView = [[UIImageView alloc] init];
        _avatarBackgroundView.layer.cornerRadius = 41;
        _avatarBackgroundView.backgroundColor = [UIColor whiteColor];
        [self addSubview:_avatarBackgroundView];
        [_avatarBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(82);
            make.center.mas_offset(0);
        }];
        
        _avatarView = [[UIImageView alloc] init];
        [self addSubview:_avatarView];
        [_avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(77);
            make.center.mas_offset(0);
        }];
        _avatarView.image = [[UIImage imageNamed:@"default_header"] sc_roundedImage];
        _avatarView.contentMode = UIViewContentModeScaleAspectFill;
        _avatarView.clipsToBounds = YES;
        _avatarButton = [[UIButton alloc] init];
        [self addSubview:_avatarButton];
        [_avatarButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(77);
            make.center.mas_offset(0);
        }];
        _mobileLabel = [[UILabel alloc] init];
        _mobileLabel.textAlignment = NSTextAlignmentCenter;
        _mobileLabel.textColor = [UIColor whiteColor];
        [self addSubview:_mobileLabel];
        [_mobileLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.avatarView.mas_bottom).offset(17);
            make.centerX.offset(0);
        }];
    }
    return self;
}

- (void)setAvatarURL:(NSURL *)avatarURL {
    [self.avatarView sd_setImageWithURL:avatarURL completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        if (image) {
            self.avatarView.image = [image sc_roundedImage];
        }
    }];
}

- (void)setMobile:(NSString *)mobile {
    if (mobile.length == 0) {
        self.mobileLabel.text = @"";
        return;
    }
    NSUInteger length = MAX(0, mobile.length - 6);
    self.mobileLabel.text = [mobile stringByCoverString:@"*" inRange:NSMakeRange(3, length)];
}

@end
